package com.kgc.search.controller;

import com.kgc.search.service.ItemSolrService;
import com.kgc.utils.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SearchController {
    @Autowired
    private ItemSolrService itemSolrService;
    @RequestMapping("search")
    public String search(String q,
                         @RequestParam(value = "page",required=false,defaultValue="1")int page,
                         Model model)throws Exception{
        String queryParam=new String(q.getBytes("iso-8859-1"),"UTF-8");
        SearchResult searchResult = itemSolrService.queryItemSolr(queryParam, page);
        model.addAttribute("query",queryParam);
        model.addAttribute("totalPage",searchResult.getTotalPages());
        model.addAttribute("itemList",searchResult.getItemList());
        return "search";
    }
}
